"""Header_Information
------------------------------------
    File_Name: ob1
    Author: RR9
    Date: 2024/10/09
    Description: 
------------------------------------
"""

from pydantic import (BaseModel, Field, field_validator)
from typing import List
from app.core.security import get_password_hash


class StudentBase(BaseModel):
    """学生基类"""
    name: str = Field(..., description="学生名字")
    sno: int = Field(..., description="学生编号")
    clas_id: int = Field(..., description="班级ID")


class StudentIn(StudentBase):
    """添加学生"""
    pwd: str = Field(min_length=6, max_length=12, description='密码')
    courses: List[int] = Field([], description="课程")

    @field_validator("name")
    @classmethod
    def validate_student(cls, v: str):
        if not v.startswith('博'):
            raise ValueError('姓名必须“博”姓')
        return v

    @field_validator("pwd")
    @classmethod
    def validate_password(cls, v: str):
        # 密码加密
        v = get_password_hash(v)
        return v


class StudentOut(StudentBase):
    """返回学生"""
    type: int = 0


# login
class Login(BaseModel):
    name: str
    password: str
